package de.multidb.imagetool;


/**
 * @(#)avgcolor.java
 *
 * avgcolor application
 *
 * @author Matheus Kwiatkowski
 * @version 1.00 2011/02/15
 */
 
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;

class Bild extends JComponent{
	Image image; int[] image_array;
	long[] avg_color_pro_segment = new long[24];
	int[] int_avg_color_pro_segment = new int[24];
	
	public Bild(){
		FileDialog dialog = new FileDialog(new Frame()); 
				dialog.setVisible(true);
				
				if(dialog.getFile() != null)
				image = getToolkit().createImage(dialog.getDirectory()+dialog.getFile());
						
		try{
			MediaTracker mt = new MediaTracker(this);
			mt.addImage(image, 0);
			mt.waitForAll();} 
		catch(Exception e){}

	}
	
	public void paintComponent(Graphics g){
		g.drawImage(image,0,0,this);
	}
	
	public Dimension getPreferredSize(){
		return getMinimumSize();
	}
	
	public Dimension getMinimumSize(){
		return new Dimension(image.getWidth(this), image.getHeight(this));
	}
	
	public void get_durchschnittsfarben(){
		image_array = new int[image.getHeight(this)*image.getWidth(this)];
		PixelGrabber grab = new PixelGrabber(image,0,0,image.getWidth(this),image.getHeight(this),image_array,0,image.getWidth(this));
		
		try{
			grab.grabPixels();
		}
		catch(Exception c){}
		
		int viertel_width= image.getWidth(this)/4;
		int sechstel_height= image.getHeight(this)/6;		
		int anzahl_pixel_pro_segment= viertel_width*sechstel_height;
		int segment_hoehe=0;
	
		
		for(int d=0; d<6; d++){
			for(int c=0; c<sechstel_height; c++){
				for(int b=0; b<4; b++){
					for(int a=0; a<viertel_width; a++){
						avg_color_pro_segment[b+segment_hoehe]=avg_color_pro_segment[b+segment_hoehe] + image_array[a + (b*viertel_width) + c*(4*viertel_width) + (4*viertel_width*sechstel_height)*d];					
					}		
				}			
			}
			segment_hoehe=segment_hoehe+4;		
		}
				
		for(int i=0; i<avg_color_pro_segment.length; i++){
			avg_color_pro_segment[i]=avg_color_pro_segment[i]/anzahl_pixel_pro_segment;
			int_avg_color_pro_segment[i]=(int)avg_color_pro_segment[i];
			
			System.out.println("Segment " + i + ": " + Integer.toHexString(int_avg_color_pro_segment[i]));
		}
				

	}
	
}
 
public class AvgColor extends JFrame{
	
	public AvgColor(){
		super("Durchschnittsfarben");
		setVisible(true);
		setLayout(new BorderLayout());
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		final Bild b = new Bild();
		JMenuBar menubar = new JMenuBar();
		setJMenuBar(menubar);
		JMenu histogramm = new JMenu("Durchschnittsfarben pro Segment");
		menubar.add(histogramm);
		JMenuItem start = new JMenuItem("Ausgeben...");
		histogramm.add(start);
		add(BorderLayout.CENTER, b);	
		pack();
		setSize(getPreferredSize());
		start.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				b.get_durchschnittsfarben();
			}
		});
	}
    
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		AvgColor avgc = new AvgColor();
		
	}
}

